package com.ctgu.springstart.controller;

import com.ctgu.springstart.domain.User;
import com.ctgu.springstart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/getUserInfo")
    public User getUser(@RequestParam(value = "username", required = true) String username) {
        return userService.findUserByName(username);
    }

    @RequestMapping("/getAllUser")
    public String getAllUser(Model model) {
        List<User> tbUsers = userService.getAllUser();
        model.addAttribute("tbUsers", tbUsers);
        return "user/list";
    }

    @RequestMapping("home")
    public String goHome(Map<String, Object> paramMap) {
        /** 默认Map的内容会放大请求域中，页面可以直接使用Thymeleaf取值*/
        paramMap.put("name", "张三");
        paramMap.put("age", 35);
        return "home";
    }

    @GetMapping("/updateUser")
    public Map updateUser(@RequestParam(value = "username", required = true) String username,
                          @RequestParam(value = "id", required = true) Integer id) {
        Map map = userService.updateUser(username, id);

        return map;
    }

}
